package com.fstech.whiteboadrd;

import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.fstech.whiteboadrd.cahrt.BarChart03View;
import com.fstech.whiteboadrd.cahrt.ChartProvider;
import com.fstech.whiteboadrd.cahrt.LineChart02View;
import com.fstech.whiteboadrd.cahrt.PieChart01View;
import com.fstech.whiteboadrd.cahrt.PieChart02View;
import com.fstech.whiteboadrd.fragment.WhiteBoardFragment;
import com.fstech.whiteboadrd.utils.UIUtils;

import org.xclcharts.chart.BarChart;
import org.xclcharts.view.ChartView;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends FragmentActivity {

    private WhiteBoardFragment whiteBoardFragment;

    boolean isSavedInstanceState = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ts = getSupportFragmentManager().beginTransaction();
        if (savedInstanceState != null) {
            whiteBoardFragment = (WhiteBoardFragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "fragment");
        }
        if (whiteBoardFragment == null) {
            whiteBoardFragment = WhiteBoardFragment.newInstance();
        }
        ts.add(R.id.fl_main, whiteBoardFragment, "wb").commit();

//        initActivity();

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        WhiteBoardFragment.rootView = null;
    }

    private void initActivity() {
        //图表的使用方法:
        //使用方式一:
        // 1.新增一个Activity
        // 2.新增一个View,继承Demo中的GraphicalView或DemoView都可，依Demo中View目录下例子绘制图表.
        // 3.将自定义的图表View放置入Activity对应的XML中，将指明其layout_width与layout_height大小.
        // 运行即可看到效果. 可参考非ChartsActivity的那几个图的例子，都是这种方式。

        //使用方式二:
        //代码调用 方式有下面二种方法:
        //方法一:
        //在xml中的FrameLayout下增加图表和ZoomControls,这是利用了现有的xml文件.
        // 1. 新增一个View，绘制图表.
        // 2. 通过下面的代码得到控件，addview即可
        //LayoutInflater factory = LayoutInflater.from(this);
        //View content = (View) factory.inflate(R.layout.activity_multi_touch, null);

        //方法二:
        //完全动态创建,无须XML文件.
        FrameLayout content = new FrameLayout(this);
        content.setBackgroundColor(UIUtils.getColor(R.color.white_pen));

        //缩放控件放置在FrameLayout的上层，用于放大缩小图表
        FrameLayout.LayoutParams frameParm = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        frameParm.gravity = Gravity.BOTTOM | Gravity.RIGHT;

		   /*
		  //缩放控件放置在FrameLayout的上层，用于放大缩小图表
	       mZoomControls = new ZoomControls(this);
	       mZoomControls.setIsZoomInEnabled(true);
	       mZoomControls.setIsZoomOutEnabled(true);
		   mZoomControls.setLayoutParams(frameParm);
		   */

        //图表显示范围在占屏幕大小的90%的区域内
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int scrWidth = (int) (dm.widthPixels * 0.9);
        int scrHeight = (int) (dm.heightPixels * 0.9);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                scrWidth, scrHeight);

        //居中显示
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        //图表view放入布局中，也可直接将图表view放入Activity对应的xml文件中
        final RelativeLayout chartLayout = new RelativeLayout(this);

//        chartLayout.addView(new BarChart03View(this), layoutParams);
//        chartLayout.addView(ChartProvider.getBarChartView(this, getData(), "王者荣耀"), layoutParams);
//        chartLayout.addView(new LineChart02View(this), layoutParams);
//        chartLayout.addView(ChartProvider.getPieChartView(this, getData(), "asdas"), layoutParams);

        chartLayout.addView(ChartProvider.getLineChartView(this, getData(), "王者荣耀"), layoutParams);

        //增加控件
        ((ViewGroup) content).addView(chartLayout);
        //((ViewGroup) content).addView(mZoomControls);
        setContentView(content);
        //放大监听
        //  mZoomControls.setOnZoomInClickListener(new OnZoomInClickListenerImpl());
        //缩小监听
        //  mZoomControls.setOnZoomOutClickListener(new OnZoomOutClickListenerImpl());
    }


    private Map<String, Double> getData() {
        Map<String, Double> data = new HashMap<>();
        data.put("阳性", 95d);
        data.put("哈性", 15d);
        data.put("安德森性", 35d);
        data.put("阳敖德萨", 195d);
        data.put("阳性是的", 295d);
        return data;
    }

}
